<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <div v-if="artObj.title===undefined">
      <van-loading color="#0094ff" size="24px" vertical>疯狂加载ing...</van-loading>
    </div>
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ artObj.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center :title="artObj.aut_name" :label="artObj.pubdate">
          <template #icon>
            <img :src="artObj.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <!-- 后台说is_follwed为false(没关注), 前台显示关注按钮 -->
              <!-- 如果is_followed为true(关注了), 显示已关注按钮 -->
              <van-button type="info" size="mini" v-if="artObj.is_followed === true" @click="followedFn(true)">已关注</van-button>
              <van-button icon="plus" type="info" size="mini" plain v-else @click="followedFn(false)">关注</van-button>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="artObj.content" v-highlight></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <!-- attitude -1没点赞, 1已点赞了 -->
          <van-button icon="good-job" type="danger" size="small" v-if="artObj.attitude === 1" @click="goodFn(-1)">已点赞</van-button>
          <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="goodFn(1)">点赞</van-button>
        </div>
      </div>
      <!-- 文章评论区域 -->
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :immediate-check="false">
        <CommentList :arr=commentList></CommentList>
      </van-list>
      <!-- 文章评论底部容器 -->
      <div :class="cmtShow ? 'art-cmt-container-1' : 'art-cmt-container-2'">
        <!-- 底部添加评论区域 - 1 -->
        <div class="add-cmt-box van-hairline--top" v-if="cmtShow">
          <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
          <div class="ipt-cmt-div" @click="cmtShow = false">发表评论</div>
          <div class="icon-box">
            <van-badge :content="totalCount" :max="99">
              <van-icon name="comment-o" size="0.53333334rem" @click="suggestFN" />
            </van-badge>
            <van-icon name="star-o" size="0.53333334rem" />
            <van-icon name="share-o" size="0.53333334rem" />
          </div>
        </div>

        <!-- 底部添加评论区域 - 2 -->
        <div class="cmt-box van-hairline--top" v-else>
          <textarea v-fofo v-model="comValue" placeholder="友善评论、理性发言、阳光心灵" @blur="blurFn"></textarea>
          <van-button type="default" :disabled="comValue.length===0" @click="sendFn" @keyup.enter="sendfn2">发布</van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { articleDetailAPI, authorFollowedAPI, authorUnFollowedAPI, addLikeAPI, commentListAPI, sendCommentAPI, articleUnLikeAPI } from '@/api/article'
import CommentList from './CommentList.vue'
// 需求: 请求文章详情
// 前置: 首页/搜索结果页, 点击ArticleItem跳转到这, 路由路径上携带aid: 文章ID值
// 1. 封装api/article.js 接口方法, 引入过来
// 2. 在created, 调用接口方法传入文章id, 拿到文章详情数据
// 3. 保存到artObj上, 到页面上铺设即可
export default {
  name: 'ArticleDetail',
  data() {
    return {
      artObj: {}, // 文章对象
      commentList: [],
      cmtShow: true,
      comValue: '',
      totalCount: '',
      loading: false, // 底部加载状态
      finished: false, // 底部完成状态
      lastId: null // 下一页(起始)评论id
    }
  },
  components: {
    CommentList
  },
  methods: {
    followedFn(bool) {
      if (bool === true) {
        // 用户点的<已关注>按钮
        // 业务 -> 取消关注
        // 调用接口 -> 取消关注接口
        // 页面要显示 -> 关注按钮
        this.artObj.is_followed = false
        authorUnFollowedAPI({
          aut_id: this.artObj.aut_id
        })
      } else {
        // 用户点的<关注>按钮
        // 业务 -> 关注
        // 调用接口 -> 关注接口
        // 页面要显示 -> 已关注按钮
        this.artObj.is_followed = true
        authorFollowedAPI({
          aut_id: this.artObj.aut_id
        })
      }
    },
    async goodFn(attitude) {
      if (attitude === -1) {
        this.artObj.attitude = -1
        await articleUnLikeAPI({
          artId: this.artObj.art_id
        })
      } else if (attitude === 1) {
        this.artObj.attitude = 1
        await addLikeAPI({
          artId: this.artObj.art_id
        })
      }
    },
    async getCommentList() {
      const resp = await commentListAPI({
        artId: this.$route.params.aid, // 拿路由的(artObj需要网络请求)
        offset: this.lastId
      })
      console.log(resp)
      if (resp.data.data.results.length === 0) {
        this.finished = true
        this.totalCount = resp.data.data.total_count || ''
        return
      }
      this.lastId = resp.data.data.last_id
      this.commentList = [...this.commentList, ...resp.data.data.results] // 数据合并

      this.loading = false
    },
    // 发布评论框 - 失去焦点
    blurFn() {
      setTimeout(() => {
        this.cmtShow = true
      })
    },
    async sendFn() {
      const resp = await sendCommentAPI({
        artId: this.artObj.art_id,
        content: this.comValue
      })
      this.commentList.unshift(resp.data.data.new_obj)
      this.comValue = ''
      this.totalCount++
      this.suggestFN()
    },
    sendfn2() {
      this.sendFn()
    },
    suggestFN() {
      document.querySelector('.like-box').scrollIntoView({ behavior: 'smooth' })
    },
    onLoad() {
      this.getCommentList()
    }
  },

  async created() {
    const res = await articleDetailAPI({
      // 接收动态路由里的传参
      art_id: this.$route.params.aid
    })
    console.log(res)
    this.artObj = res.data.data
    this.getCommentList()
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}
.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
.van-loading {
  padding-top: 46px;
}
</style>
